import Vue from 'vue';
import VueRouter from 'vue-router';
// const home = () => import('@/views/home');
// const about = () => import('@/views/about');
// const user = () => import('@/views/user');
import home from '@/views/home';
// import { cosh } from 'core-js/core/number';
// import about from '@/views/about';
// import user from '@/views/user';
// @相当于从src开始找文件

Vue.use(VueRouter); //要把vuerouter在vue里面实例化一下

let router = new VueRouter({
    routes: [
        {
            // 默认跳转的地址
            path: '/',
            // redirect:"当路径为/时，默认跳转的路径"
            redirect: '/home'
        },
        {
            // path:"路径地址"
            path: '/home',
            // component:需要跳转的组件
            component: home,
            meta: {
                title: '首页'
            },
            children: [
                {
                    // 默认跳转的地址
                    path: '',
                    // redirect:"当路径为/时，默认跳转的路径"
                    redirect: 'two'
                },
                {
                    path: 'three',
                    component: () => import('@/views/components/home-cpn-three')
                },
                {
                    path: 'two',
                    component: () => import('@/views/components/home-cpn-two'),
                    children: [
                        {
                            path: '',
                            redirect: 'four'
                        },
                        {
                            path: 'four',
                            component: () => import('@/views/components/home-cpn-four'),
                            meta: {
                                title: '组件4'
                            }
                        },
                        {
                            path: 'five',
                            component: () => import('@/views/components/home-cpm-five'),
                            meta: {
                                title: '组件5'
                            }
                        }
                    ]
                }
            ]
        },
        {
            path: '/about',
            component: () => import('@/views/about')
        },
        {
            path: '/user/:id',
            component: () => import('@/views/user')
        },
        {
            path: '/msg',
            component: () => import('@/views/msg'),
            meta: {
                title: '接收'
            },
            // 路由独享守卫
            beforeEnter: (to, from, next) => {
                console.log('你进入了接收页面');
                next();
            }
        },
        {
            path: '/get',
            component: () => import('@/views/get'),
            meta: {
                title: '传值'
            }
        }
    ],
    linkActiveClass: 'active',
    mode: 'history'
});
router.afterEach((to, from) => {
    console.log('跳转完成执行');
});

//全局路由守卫
router.beforeEach((to, from, next) => {
    console.log('跳转前执行');
    // to是要去的地方
    // from是离开的地方
    window.document.title = to.meta.title;
    next();
    // if ('登录了') {
    //     next();
    // } else {
    //     next('/login');
    // }
});

export default router;
